//
//  UserAccount.swift
//  weibo
//
//  Created by apple on 15/12/14.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
    
//    用于调用access_token，接口获取授权后的access token
    var access_token: String?
    
//    access_token的生命周期，单位是秒数
    var expires_in: NSTimeInterval = 0 {

        didSet {
            
            //当过期时间一旦被设置的时候  就立即设置expires_date\
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
            
        }
        
    }
    
//	  当前授权用户的UID。
    var uid: String?
    
//   access_token 的过期日期
    var expires_date: NSDate?
    
// 	  友好显示名称
    var name: String?

//    用户头像地址（大图），180×180像素
    var avatar_large: String?
    
//    kvc赋值
    init(dict: [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
//    防止获取的字典的key 不匹配  崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
//    
  override var description: String {
    
    let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
    
      return String(dictionaryWithValuesForKeys(keys))
    
    }
    
//    保存自定义对象
    func saveAccount() {
//      保存到沙盒的地址
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
//        print(path)
        
//      归档保存
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        
    }
    
//    去沙盒里面读取账户信息
    class func loadAccount() -> UserAccount? {
        
//        读取文件沙盒路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {

          //  需要判断有效期
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
            return account
                
            }
        }
            return nil
    }
    
//    归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        
        aCoder.encodeObject(uid, forKey: "uid")
        
        aCoder.encodeObject(name, forKey: "name")
        
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
    }
    
//    解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        name = aDecoder.decodeObjectForKey("name") as? String
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        
    }

    
}
